using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

using NBusiness.CodeDom;
using NBusiness.CodeDom.Compiler;
using NBusiness;
using NBusiness.ESharp;

using NBusiness.ESharp.Compiler;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.ESharp.Testing.Properties;
using NBusiness.ESharp.Testing.Mocks;
using NBusiness.CodeDom.Services;

namespace NBusiness.ESharp.Testing
{
	[TestClass]
	public class AdvancedTests
	{
        [TestMethod]
		public void AdvancedRelationshipTests()
		{
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Advanced.Person.es", Resources.Advanced_Person ),
                new EntityFile ("Advanced.Ssn.es", Resources.Advanced_Ssn ),
                new EntityFile ("Advanced.Address.es", Resources.Advanced_Address ));

            Assert.AreEqual(0, errorService.Errors.Count);

            Entity Person = root.Families["Advanced"].Entities[0];
            Entity Ssn = root.Families["Advanced"].Entities[1];

            EntityField PersonSsnValue = Person.Fields[3];
            EntityField PersonSsnIssued = Person.Fields[4];
            EntityRelationship PersonSsn = Person.Relationships[0];

            EntityField SsnValue = Ssn.Fields[0];
            EntityField SsnIssued = Ssn.Fields[1];

            Assert.AreSame(Ssn, Person.Relationships[0].With);
            Assert.IsTrue(PersonSsn.On.ContainsKey(PersonSsnValue));
            Assert.IsTrue(PersonSsn.On.ContainsKey(PersonSsnIssued));
            Assert.AreSame(PersonSsn.On[PersonSsnValue], SsnValue);
            Assert.AreSame(PersonSsn.On[PersonSsnIssued], SsnIssued);
		}

		[TestMethod]
		public void AdvancedRelationshipTests2()
		{
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Advanced.Person.es", Resources.Advanced_Person ),
                new EntityFile ("Advanced.Ssn.es", Resources.Advanced_Ssn ));

            //Relative entity is not included which results in errors
            Assert.AreEqual(2, errorService.Errors.Count);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[0].Type);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[1].Type);

            Assert.AreEqual(ESharpEntityElementTypes.RelationshipAs, errorService.Errors[0].Element.Type);
            Assert.AreEqual(ESharpEntityElementTypes.RelationshipDeclaration, errorService.Errors[1].Element.Type);
		}
	}
}
